import time

from Common.Base import Base, open_browser

register_url = 'http://172.16.11.209:8080/ecshop/user.php?act=register'


class RegisterPage(Base):
    """封装表现层: 制作定位器"""
    username_loc = ('name', 'username')  # 用户名输入框
    username_notice_loc = ('id', 'username_notice')  # 用户名输入框提示
    email_loc = ('name', 'email')  # 邮箱输入框
    email_notice = ('id', 'email_notice')  # 邮箱输入框提示
    password_loc = ('name', 'password')  # 密码输入框
    password_loc_notice = ('id', 'password_notice')  # 密码输入框提示
    confirm_password_loc = ('name', 'confirm_password')  # 确认密码输入框
    confirm_password_loc_notice = ('id', 'conform_password_notice')  # 确认密码输入框提示
    qq_loc = ('name', 'extend_field2')  # qq号码输入框
    office_phone_loc = ('name', 'extend_field3')  # 办公电话输入框
    home_phone_loc = ('name', 'extend_field4')  # 家庭电话输入框
    mobile_phone_loc = ('name', 'extend_field5')  # 手机输入框
    password_question_loc = ('css selector', 'select[name="sel_question"] option:nth-of-type(7)')  # 密码提示问题
    password_answer_loc = ('name', 'passwd_answer')  # 密码问题答案
    checkbox_loc = ('name', 'agreement')  # 我以看过并接受复选框
    register_loc = ('name', 'Submit')  # 立即注册按钮
    user_agreement = ('link text', '用户协议')  # 用户协议链接
    login_loc = ('link text', '我已有账号，我要登录')  # 我已有账号,我要登录链接
    forget_password_loc = ('link text', '您忘记密码了吗？')  # 您忘记密码了吗?按钮
    result_loc = ('class name', 'f4_b')  # 登录后用户名

    def input_username(self, username):
        """
        输入用户名
        :param username: 用户名
        :return:
        """
        self.send_keys(self.username_loc, username)

    def get_username_notice(self):
        """获取用户名提示"""
        time.sleep(6)
        return self.get_element_text(self.username_notice_loc)

    def input_email(self, email):
        """
        输入邮箱
        :param email: 邮箱
        :return:
        """
        self.send_keys(self.email_loc, email)

    def get_email_notice(self):
        """获取email提示"""
        time.sleep(6)
        return self.get_element_text(self.email_notice)

    def input_password(self, password):
        """
        输入密码
        :param password: 密码
        :return:
        """
        self.send_keys(self.password_loc, password)

    def get_password_notice(self):
        """获取密码提示信息"""
        time.sleep(6)
        return self.get_element_text(self.password_loc_notice)

    def input_confirm_password(self, password):
        """
        确认密码
        :param password: 密码
        :return:
        """
        self.send_keys(self.confirm_password_loc, password)

    def get_conform_password_notice(self):
        """获取确认密码输入框提示信息"""
        time.sleep(6)
        return self.get_element_text(self.confirm_password_loc_notice)

    def input_qq(self, qq):
        """
        输入qq
        :param qq: qq号码
        :return:
        """
        self.send_keys(self.qq_loc, qq)

    def input_office_phone(self, phone):
        """
        输入办公电话
        :param phone: 电话号码
        :return:
        """
        self.send_keys(self.office_phone_loc, phone)

    def input_home_phone(self, phone):
        """
        输入家庭电话
        :param phone: 电话号码
        :return:
        """
        self.send_keys(self.home_phone_loc, phone)

    def input_mobile_phone(self, phone):
        """
        输入手机号码
        :param phone: 电话号码
        :return:
        """
        self.send_keys(self.mobile_phone_loc, phone)

    def click_question(self):
        """
        选择密码提示问题
        :return:
        """
        self.click(self.password_question_loc)

    def input_answer(self, answer):
        """
        输入密码问题答案
        :param answer: 答案
        :return:
        """
        self.send_keys(self.password_answer_loc, answer)

    def window_scroll(self, number):
        """
        滚动条向下滚动
        :return:
        """
        js_down = f'window.scrollTo(0,{number})'
        self.driver.execute_script(js_down)

    def click_checkbox(self):
        """
        勾选复选框
        :return:
        """
        checkbox = self.driver.find_element_by_name('agreement')
        if checkbox.is_selected():
            pass
        else:
            self.click(self.checkbox_loc)

    def click_register(self):
        """
        点击注册按钮
        :return:
        """
        self.click(self.register_loc)

    def click_user_agree(self):
        """
        用户协议链接
        :return:
        """
        self.click(self.user_agreement)

    def click_login(self):
        """
        点击'我已有账号,我要登录'
        :return:
        """
        self.click(self.login_loc)

    def click_forget_password(self):
        """
        点击'您忘记密码了吗?'
        :return:
        """
        self.click(self.forget_password_loc)

    def is_login_success(self, username):
        """
        判断注册是否成功,如果成功,返回True,反之返回False
        :param username:
        :return:
        """
        return self.is_text_equal_in_element(self.result_loc, username)

    def get_login_username(self):
        """
        获取注册后的用户名
        :return:
        """
        return self.get_element_text(self.result_loc)

    def is_click_success(self, text):
        """
        判断是否链接跳转成功
        @param text: 原窗口url
        @return:
        """
        return self.is_url_equal(text)




if __name__ == '__main__':
    registerpage = RegisterPage(open_browser())
    registerpage.open_url('http://localhost:8080/ecshop/user.php?act=register')
    # registerpage.input_username('name1')
    # registerpage.input_email('2034384295@qq.com')
    # registerpage.input_password('123456')
    # registerpage.input_confirm_password('123456')
    # registerpage.input_qq('2058469854')
    # registerpage.input_office_phone('12548665236')
    # registerpage.input_home_phone('12548665236')
    # registerpage.input_mobile_phone('12548665236')
    # registerpage.click_question()
    # registerpage.input_answer('橘子')
    # registerpage.window_scroll('500')  # 滚动条向下滚动
    # registerpage.click_checkbox()
    # registerpage.click_register()
    # # registerpage.click_login()
    # # registerpage.click_forget_password()
    print(registerpage.get_url())
    registerpage.close_browser()
